<?php
class table{
	var $fun;//将调用的方法名以,号拼接成一个字符串
	var $w;//存储where语句的变量
	var $p;//存储limit语句的变量
	var $o;//存储order by语句的变量
	var $f;//存储查看字段的变量
	var $tbname;//表名
	var $sqllj;//数据库链接
	
	/**
	 * conn	数据库链接
	 * tn	表名
	 * db	库名
	 */
	function __construct($conn,$tn){
		$this->sqllj = $conn;
		$this->tbname = $tn;
		
	}
	
	/**
	 * 自定义sql语句执行方法
	 * sql	sql语句
	 * conn	w或r
	 */
	function _query($sql,$conn){
		$result = mysql_query($sql,$this->sqllj["$conn"]);
		return $result;
	}
	
	/**
	 * 将被调用的方法名拼成的字符串分解成数组
	 */
	function funstart(){
		$ab = array_values(array_filter(explode('.',$this->fun)));
		return $ab;
	}
	
	/**
	 * 查询
	 */
	function s(){
		$f = $this->funstart();
		if(empty($this->f)){
			$w = 'select * from '.$this->tbname;//查询语句
		}else{
			$w = 'select '.$this->f.' from '.$this->tbname;//查询语句
		}
		$a = '';
		$b = '';
		foreach($f as $k => $v){
			if($v == 'w' && $this->w != ''){
				$a = ' where '.$this->w;
				$this->w = '';
			}elseif($v == 'o' && $this->o != ''){
				$b = ' '.$this->o;
				$this->o = '';
			}
		}
		$w = $w.$a.$b.' limit 1';
		$d = $this->_query($w,'r');
		if($d){
			$e = mysql_num_rows($d);
			if($e == 0){
				$r = '';
			}else{
				$r = mysql_fetch_assoc($d);
				mysql_free_result($d);//释放结果集内存
			}
			//mysql_close($this->sqllj["r"]);//关闭数据库
			return $r;
		}else{
			return ;
		}
	}
	
	/**
	 * 查询多条
	 */
	function ss(){
		$f = $this->funstart();
		if(empty($this->f)){
			$w = 'select * from '.$this->tbname;//查询语句
		}else{
			$w = 'select '.$this->f.' from '.$this->tbname;//查询语句
		}
		$a = '';
		$b = '';
		$c = '';
		foreach($f as $k => $v){
			if($v == 'w' && $this->w != ''){
				$a = ' where '.$this->w;
				$this->w = '';
			}elseif($v == 'o' && $this->o != ''){
				$b = ' '.$this->o;
				$this->o = '';
			}elseif($v == 'p' && $this->p != ''){
				$c = $this->p;
				$this->p = '';
			}
		}
		$w = $w.$a.$b.$c;
		$d = $this->_query($w,'r');
		if($d == ''){
			return '';
		}else{
			$e = mysql_num_rows($d);
			if($e == 0){
				$r = '';
			}else{
				for($i=0;$i<$e;$i++){
					$r[] = mysql_fetch_assoc($d);
				}
			}
			mysql_free_result($d);//释放结果集内存
			//mysql_close($this->sqllj['r']);//关闭数据库
			return $r;
		}
	}
	
	/**
	 * 写入
	 * c	数组,要写入的数据
	 */
	function c($c){
		$m = '';
		$n = '';
		foreach($c as $k => $v){
			$v = addslashes($v);
			$m = $m."`$k`".',';//将列名拼接起来
			$n = $n."'$v'".',';//将与列名对应的值拼接起来
		}
		$m = substr($m,0,strlen($m)-1);//去掉最后一个,号
		$n = substr($n,0,strlen($n)-1);//去掉最后一个,号
		$c = 'insert into '.$this->tbname.' ('.$m.') values ('.$n.')';
		$d = $this->_query($c,'w');
		$e = mysql_insert_id($this->sqllj['w']);//获取最新写入数据的id
		//mysql_close($this->sqllj['w']);//关闭数据库
		if($d == '1'){
			return $e;
		}else{
			return ;
		}
	}
	
	/**
	 * 更新
	 * u	数组,要更新的数据
	 */
	function u($u){
		$f = $this->funstart();
		$m = '';
		$zf = array('+','-');
		foreach($u as $k => $v){
			$v = addslashes($v);
			$z = substr($k,-1);//+/-
			if(in_array($z,$zf) && is_numeric($v)){//如果是$k+=>1或者$k-=>1
				$k = substr($k,0,-1);//$k去除+/-
				$v = $k.$z."'$v'";
			}else{
				$v = "'$v'";
			}
			$m .=',`'.$k.'` = '.$v;//将更新条件拼接起来
		}
		$m = ($m != '') ? substr($m,1) : '';//去掉第一个,号
		$u = 'update '.$this->tbname.' set '.$m;
		$a = '';
		foreach($f as $k => $v){
			if($v == 'w' && $this->w != ''){
				$a = ' where '.$this->w;
				$this->w = '';
			}
		}
		$u = $u.$a;
		$d = $this->_query($u,'w');
		//mysql_close($this->sqllj['w']);//关闭数据库
		return $d;
	}
	
	/**
	 * 删除
	 */
	function d(){
		$f = $this->funstart();
		$w = 'select * from '.$this->tbname;//查询语句
		$a = '';
		foreach($f as $k => $v){
			if($v == 'w' && $this->w != ''){
				$a = ' where '.$this->w;
			}
		}
		$w = $w.$a.' limit 1';
		$b = $this->_query($w,'r');
		$c = mysql_num_rows($b);
		if($c == 0){
			$e = '';
		}else{
			$e = mysql_fetch_assoc($b);
		}
		if($e){
			$d = 'delete from '.$this->tbname.' where id = '.$e['id'];
			$r = $this->_query($d,'w');
			//mysql_close($this->sqllj['w']);//关闭数据库
			return $r;
		}else{
			//mysql_close($this->sqllj['w']);//关闭数据库
			return ;
		}
	}
	
	/**
	 * 删除全部
	 */
	function del_all(){
		$f = $this->funstart();
		$d = 'delete from '.$this->tbname;
		$a = '';
		foreach($f as $k => $v){
			if($v == 'w' && $this->w != ''){
				$a = ' where '.$this->w;
				$this->w = '';
			}
		}
		$d = $d.$a;
		$r = $this->_query($d,'w');
		//mysql_close($this->sqllj['w']);//关闭数据库
		return $r;
	}
	
	/**
	 * 返回符合条件数据的条数
	 * c	列名
	 */
	function n($c = '*'){
		$f = $this->funstart();
		$a = '';
		foreach($f as $k => $v){
			if($v == 'w' && $this->w != ''){
				$a = ' where '.$this->w;
				$this->w = '';
			}
		}
		$n = 'select count('.$c.') from '.$this->tbname.$a;
		$d = $this->_query($n,'r');
		$e = @mysql_num_rows($d);
		if($e == 0){
			//mysql_close($this->sqllj['r']);//关闭数据库
			return ;
		}else{
			$r = mysql_fetch_assoc($d);
			//mysql_close($this->sqllj['r']);//关闭数据库
			return $r['count('.$c.')'];
		}
	}
	
	/**
	 * where语句
	 * post	数组
	 */
	function w($post,$t = 0){
		if($post && is_array($post)){
			$m = array();
			foreach($post as $k => $v){
				$fh = substr($k,-1);
				if(is_array($v)){
					if($fh == '?'){//模糊搜索多个值
						$k = substr($k,0,-1);
						$zhi = '(';
					}
					foreach ($v as $kk=>$vv){
						if($fh == '?'){//模糊搜索多个值
							$zhi .= "`$k` LIKE '%$vv%'"." OR ";
						}else{
							$v[$kk] = "'{$vv}'";
						}
					}
					if($fh == '?'){//模糊搜索多个值
						$zhi = substr($zhi,0,-4);
						$m[] = $zhi.')';
					}elseif($fh == '!'){
						$k = substr($k,0,-1);
						$m[] = "`$k` NOT IN (".implode(',',$v).")";
					}else{
						$m[] = "`$k` IN (".implode(',',$v).")";
					}
				}else{
					if($fh == ']'){
						$k = substr($k,0,-1);
						$m[] = "`$k`>='$v'";
					}elseif($fh == '|'){
						$k = substr($k,0,-1);
						$m[] = "`$k`<='$v'";
					}elseif($fh == '>'){
						$k = substr($k,0,-1);
						$m[] = "`$k`>'$v'";
					}elseif($fh == '<'){
						$k = substr($k,0,-1);
						$m[] = "`$k`<'$v'";
					}elseif($fh == '!'){
						$k = substr($k,0,-1);
						$m[] = "`$k`<>'$v'";
					}elseif($fh == '?'){
						$k = substr($k,0,-1);
						$m[] = "`$k` LIKE '%$v%'";
					}elseif($fh == '('){//以$v结尾
						$k = substr($k,0,-1);
						$m[] = "`$k` LIKE '%$v'";
					}elseif($fh == ')'){//以$v开头
						$k = substr($k,0,-1);
						$m[] = "`$k` LIKE '$v%'";
					}else{
						$m[] = "`$k` = '$v'";
					}
				}
			}
			$m = implode(' and ',$m);
		}else{
			$m = $post;
		}
		if($t) return $m;
		if($m){
			$this->w = $m;
			$this->fun = $this->fun.'.w';
		}
		return $this;
	}
	
	/**
	 * where and语句
	 * post 数组
	 */
	function wa($post){
		if($this->w){
			$sql = $this->w($post,1);
			if($sql){
				$this->w .= ' and '.$sql;
				$this->fun = $this->fun.'.w';
			}
			return $this;
		}else{
			return $this->w($post);
		}
	}
	
	/**
	 * where or语句
	 * post 数组
	 */
	function wo($post){
		if($this->w){
			$sql = $this->w($post,1);
			if($sql){
				$this->w .= ' or '.$sql;
				$this->fun = $this->fun.'.w';
			}
			return $this;
		}else{
			return $this->w($post);
		}
	}
	
	/**
	 * 选取一条或多条数据
	 * page		页数
	 * pagesize	选取数据的条数
	 */
	function p($page,$pagesize){
		$this->p = ' limit '.($page-1)*$pagesize.','.$pagesize;
		$this->fun = $this->fun.'.p';
		return $this;
	}
	
	/**
	 * 根据列名进行排序
	 * name	列名
	 * sx	排序方式(desc从大到小,asc从小到大)
	 */
	function o($name,$sx='desc'){
		$this->o = 'order by '.$name.' '.$sx;		
		$this->fun = $this->fun.'.o';
		return $this;
	}
	
	/**
	 * 根据列名查找数据
	 * f	列名
	 */
	function f($f){
		if(is_array($f)){
			$this->f = implode(',',$f);
		}else{
			$this->f = $f;
		}
		return $this;
	}
	
	/**
	 * 复制表
	 * $biao like 表名
	 */
	function like($biao){
		$sql = "CREATE TABLE ".$this->tbname." LIKE ".$biao;//复制表结构到新表
		$rs = $this->_query($sql,'w');
		return $rs;
	}
	/**
	 * 删除表
	 */
	function drop(){
		$sql = "DROP TABLE ".$this->tbname;//删除数据表
		$rs = $this->_query($sql,'w');
		return $rs;
	}
	
	/**
	 * 查看表
	 */
	function ckan(){
		$sql = "SHOW TABLES LIKE '".$this->tbname."'";
		$rs = mysql_num_rows($this->_query($sql,'w'));
		return $rs;
	}
	
	/**
	 * 添加列
	 * $lie 添加列名
	 * $lx 添加列的类型
	 */
	function add($lie,$lx){
		$sql = "ALTER TABLE ".$this->tbname." ADD ".$lie." ".$lx;//添加列
		$rs = $this->_query($sql,'w');
		return $rs;
	}
	
	/**
	 * 删除列
	 * $lie 删除列名
	 */
	function dropc($lie){
		$sql = "ALTER TABLE ".$this->tbname." DROP COLUMN ".$lie;//删除列
		$rs = $this->_query($sql,'w');
		return $rs;
	}
	
	/**
	 * 获取表中字段备注
	 * $lie 字段名
	 */
	function comment($lie){
		$sql = "select column_comment from information_schema.columns where table_name='".$this->tbname."' and column_name='".$lie."'";
		$r = $this->_query($sql,'r');
		$rs = mysql_fetch_array($r);
		return $rs['column_comment'];
	}
	
	
}
/*********************************************************************文件已结束*****************************************************************/